package com.example.courselab.ui.feature.home

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.domain.model.Product
import com.example.domain.network.ResultWrapper
import com.example.domain.usecase.GetCategoriesUseCase
import com.example.domain.usecase.GetProductUseCase
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch

class HomeViewModel(private val getProductUseCase: GetProductUseCase,
                    private val categoryUseCase: GetCategoriesUseCase) : ViewModel() {

  private val _uiState = MutableStateFlow<HomeScreenEvent>(HomeScreenEvent.Loading)
  val uiState = _uiState.asStateFlow()

  init {
    getAllProducts()
  }

  private fun getAllProducts() {
    viewModelScope.launch {
      _uiState.value = HomeScreenEvent.Loading
      val featured = getProducts("electronics")
      val popularProducts = getProducts("jewelery")
      val categories = getCategory()
//      if (featured.isEmpty() || popularProducts.isEmpty()) {
      if (featured.isEmpty() && popularProducts.isEmpty() && categories.isNotEmpty()) {
        _uiState.value = HomeScreenEvent.Error("Failed to load products")
        return@launch
      }
      _uiState.value = HomeScreenEvent.Success(featured, popularProducts, categories)
    }
  }

  private suspend fun getProducts(category: String?): List<Product> {
    getProductUseCase.execute(category).let { result ->
      when (result) {
        is ResultWrapper.Success -> {
          return (result).value
        }

        is ResultWrapper.Failure -> {
          return emptyList()
        }
      }
    }
  }

  private suspend fun getCategory(): List<String> {
    categoryUseCase.execute().let { result ->
      when (result) {
        is ResultWrapper.Success -> {
          return (result).value
        }

        is ResultWrapper.Failure -> {
          return emptyList()
        }
      }
    }
  }
}

sealed class HomeScreenEvent {
  data object Loading : HomeScreenEvent()
  data class Success(
    val featured: List<Product>,
    val popularProducts: List<Product>,
    val categories: List<String>) :
    HomeScreenEvent()
  data class Error(val message: String) : HomeScreenEvent()
}